package diploma.kisel.modelas.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.SpringLayout;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JButton;

import diploma.kisel.modelas.gui.common.CurrentVar;
import diploma.kisel.modelas.gui.common.TablesSettings;
import diploma.kisel.modelas.service.WorkstationServiceImpl;
import diploma.kisel.modelas.service.api.WorkplaceService;
import diploma.kisel.modelas.service.api.WorkstationService;
import javax.swing.ScrollPaneConstants;
import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * This window is used to show servers from current concept.
 * 
 * @author Nikolay Kisel
 *
 */
public class ServerCatalogWindow extends JFrame {

	private JPanel contentPane;
	private JTable table;

	private static DefaultTableModel model;

	private WorkstationService workstationService = new WorkstationServiceImpl();
	
	private static ServerCatalogWindow frame;

	/**
	 * Create the frame.
	 */
	public ServerCatalogWindow() {
		setTitle("Серверы");
//		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 314);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		SpringLayout sl_contentPane = new SpringLayout();
		contentPane.setLayout(sl_contentPane);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setAlignmentY(Component.BOTTOM_ALIGNMENT);
		scrollPane.setAlignmentX(Component.RIGHT_ALIGNMENT);
		sl_contentPane.putConstraint(SpringLayout.NORTH, scrollPane, 0,
				SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, scrollPane, 0,
				SpringLayout.WEST, contentPane);
		contentPane.add(scrollPane);

		table = new JTable(
				TablesSettings.modelSettingForServers(model, workstationService
						.findByConcept(CurrentVar.getCurrentConcept())));
		scrollPane.setViewportView(table);
		TablesSettings.makeFirstColumnInvisible(table);

		JButton button = new JButton("Отмена");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				frame.setVisible(false);
			}
		});
		sl_contentPane.putConstraint(SpringLayout.SOUTH, scrollPane, -19,
				SpringLayout.NORTH, button);
		sl_contentPane.putConstraint(SpringLayout.EAST, scrollPane, 0,
				SpringLayout.EAST, button);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, button, 0,
				SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, button, -5, SpringLayout.EAST, contentPane);
		contentPane.add(button);
	}

	public static ServerCatalogWindow getFrame() {
		return frame;
	}

	public static void setFrame(ServerCatalogWindow frame) {
		ServerCatalogWindow.frame = frame;
	}	
	
}
